package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lawyer.entity.Config;
import com.lawyer.entity.HomePic;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.HomePicRepository;
import com.lawyer.service.FileUpLoadService;
import com.lawyer.uitls.BeanUtils;

@RestController
@RequestMapping(value = "/system")
@Api(tags = "系统管理-首页")
@SuppressWarnings("all")
public class SystemHomePageController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private HomePicRepository homePicRepository;

	@Autowired
	FileUpLoadService fileUpLoadService;

	@ApiOperation(httpMethod = "GET", value = "首页轮播图列表", notes = "首页轮播图列表")
	@RequestMapping(value = "/queryPagePic", method = RequestMethod.GET)
	public HttpEntity<?> queryPagePic() {
		Message msg = new Message();
		msg.setData(homePicRepository.findAll());
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	
	@ApiOperation(httpMethod = "GET", value = "轮播图详情", notes = "轮播图详情")
	@RequestMapping(value = "/pagePicInfo", method = RequestMethod.GET)
	public HttpEntity<?> pagePicInfo(Integer id) {
		Message msg = new Message();
		msg.setData(homePicRepository.findOne(id));
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	@ApiOperation(httpMethod = "POST", value = "新增/修改轮播图", notes = "新增/修改轮播图")
	@RequestMapping(value = "/updatePagePic", method = RequestMethod.POST)
	public HttpEntity<?> updateConfig(@RequestBody HomePic homePic) {
		Message msg = new Message();
		try {
			if (homePic.getId() != null) {
				//修改
				HomePic findHome = homePicRepository.findOne(homePic.getId());
				BeanUtils.copyPropertiesIgnoreNullValue(homePic, findHome);
				homePicRepository.save(findHome);
			}else{
				//新增
				homePicRepository.save(homePic);
			}
			

		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	@ApiOperation(httpMethod = "DELETE", value = "删除轮播图", notes = "删除轮播图")
	@RequestMapping(value = "/pagePicDelete", method = RequestMethod.DELETE)
	public HttpEntity<?> pagePicDelete(Integer id) {
		Message msg = new Message();
		homePicRepository.delete(id);
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

}
